package com.cloud.gateway.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cloud.common.BaseReturn;
import com.cloud.common.CloudUserUtils;
import com.cloud.server.data.user.StudentData;
import com.cloud.server.domain.user.User;
import com.cloud.server.domain.user.UserIntegral;
import com.cloud.server.domain.user.UserRole;
import com.cloud.server.service.user.UserIntegralService;
import com.cloud.server.service.user.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author liuyuan
 * @Date 2021/11/28 1:40 下午
 * @Version 1.0
 */
@RestController
@RequestMapping("/api")
public class StudentController {
    @Autowired
    UserService userService;
    @Autowired
    UserIntegralService userIntegralService;

    @RequestMapping("/getStudent")
    public BaseReturn getStudent(){
        Long userId = null;
        try {
            userId = CloudUserUtils.getUserId();
            User user = userService.getById(userId);
            String idClass = user.getIdClass();
            User queryUser = new User();
            queryUser.setIdClass(idClass);
            queryUser.setUserType(UserRole.STUDENT);
            QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
            userQueryWrapper.setEntity(queryUser);
            List<StudentData> studentDataList = new ArrayList<>();
            List<User> lists = userService.list(userQueryWrapper);
            for (User list : lists) {
                StudentData studentData = new StudentData();
                Long id = list.getId();
                UserIntegral userIntegral = userIntegralService.getByUserId(id);
                BeanUtils.copyProperties(list,studentData);
                BeanUtils.copyProperties(userIntegral,studentData);
                studentDataList.add(studentData);
            }
            return BaseReturn.build("200","查询成功",studentDataList);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseReturn.build("500",e.getMessage());

        }
    }
}
